package com.application.examples.entity;

import java.io.Serializable;

/**
 * @author xiongwei_vendor
 * @date 2020/11/25 19:02
 */
public class BaseResponse<T> implements Serializable {

    private static final long serialVersionUID = 1L;
    private String code;
    private String msg;
    private T data;

    public BaseResponse() {
    }

    public BaseResponse(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public BaseResponse(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public BaseResponse(ResultCode resultCode) {
        this.code = resultCode.getCode();
        this.msg = String.format(resultCode.getMsg(), "");
    }

    public BaseResponse(ResultCode resultCode, String message) {
        this.code = resultCode.getCode();
        this.msg = String.format(resultCode.getMsg(), message);
    }

    public static <T> BaseResponse<T> success(T data) {
        BaseResponse<T> response = new BaseResponse();
        response.code = CobraResultCodeConstant.SUCCESS.getCode();
        response.msg = CobraResultCodeConstant.SUCCESS.getMsg();
        response.data = data;
        return response;
    }

    public static BaseResponse fail(ResultCode resultCode) {
        BaseResponse response = new BaseResponse();
        response.code = resultCode.getCode();
        response.msg = String.format(resultCode.getMsg(), "");
        return response;
    }

    public static BaseResponse fail(ResultCode resultCode, String message) {
        BaseResponse response = new BaseResponse();
        response.code = resultCode.getCode();
        response.msg = String.format(resultCode.getMsg(), message);
        return response;
    }

    public static BaseResponse fail(BaseResponse baseResponse) {
        BaseResponse response = new BaseResponse();
        response.code = baseResponse.getCode();
        response.msg = baseResponse.getMsg();
        return response;
    }

    public boolean isSuccess() {
        return CobraResultCodeConstant.SUCCESS.getCode().equals(this.code);
    }

    @Override
    public String toString() {
        return "BaseResponse{code='" + this.code + '\'' + ", msg='" + this.msg + '\'' + ", data=" + this.data + '}';
    }

    public String getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public T getData() {
        return this.data;
    }

    public void setCode(final String code) {
        this.code = code;
    }

    public void setMsg(final String msg) {
        this.msg = msg;
    }

    public void setData(final T data) {
        this.data = data;
    }

    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof BaseResponse)) {
            return false;
        } else {
            BaseResponse<?> other = (BaseResponse) o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label47:
                {
                    Object this$code = this.getCode();
                    Object other$code = other.getCode();
                    if (this$code == null) {
                        if (other$code == null) {
                            break label47;
                        }
                    } else if (this$code.equals(other$code)) {
                        break label47;
                    }

                    return false;
                }

                Object this$msg = this.getMsg();
                Object other$msg = other.getMsg();
                if (this$msg == null) {
                    if (other$msg != null) {
                        return false;
                    }
                } else if (!this$msg.equals(other$msg)) {
                    return false;
                }

                Object this$data = this.getData();
                Object other$data = other.getData();
                if (this$data == null) {
                    if (other$data != null) {
                        return false;
                    }
                } else if (!this$data.equals(other$data)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(final Object other) {
        return other instanceof BaseResponse;
    }

    @Override
    public int hashCode() {
        boolean PRIME = true;
        int result = 1;
        Object $code = this.getCode();
        result = result * 59 + ($code == null ? 43 : $code.hashCode());
        Object $msg = this.getMsg();
        result = result * 59 + ($msg == null ? 43 : $msg.hashCode());
        Object $data = this.getData();
        result = result * 59 + ($data == null ? 43 : $data.hashCode());
        return result;
    }

}
